#pragma once
#include <string>
#include <modbus/modbus.h>
#include <iostream>
#include <stdexcept>
#include <sys/time.h>
#include <chrono>
#include <thread>
#include <atomic>
#include "ModBusDefine.h"

class ModBusProcess;

struct modbusConnectInfo{
    std::string strDevice;      //端口号
    int nBaudRate;              //波特率
    char cParity;        //校验方式
    int nDataBit;
    int nStopBit;

    modbusConnectInfo():
    strDevice("/dev/ttyS0"),
    nBaudRate(9600),
    cParity('N'),
    nDataBit(8),
    nStopBit(1)
    {};
    
    void operator=(const modbusConnectInfo& info){
        this->strDevice = info.strDevice;
        this->nBaudRate = info.nBaudRate;
        this->cParity = info.cParity;
        this->nDataBit = info.nDataBit;
        this->nStopBit = info.nStopBit;
    };
};

class ModBusCollectManager
{
private:
    /* data */
public:
    ModBusCollectManager(/* args */);
    ~ModBusCollectManager();

    void init(const modbusConnectInfo&);
    void start();
    void stop();
private:
    bool connect(bool bReconnect = false, int nReTimes = 10);
    void collect();

private:
    modbusConnectInfo _modbusConInfo;
    modbus_t *mb;
    std::atomic<bool> mQuit;
    ModBusProcess *processPtr;
    std::thread threadProcess;
};

